package com.kc.gb.device.configurations;

import com.kc.gb.device.common.utils.I18NHelper;
import com.kc.gb.device.configurations.support.RequestResponseLoggingFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.List;
import java.util.Locale;

/**
 * @author : evan.liu
 * @version V1.0
 * @Description:
 * @date Date : 2022年05月01日 1:32 上午
 */
@Slf4j
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 打印所有消息转换器
        converters.forEach(converter -> {
            // log.info("HttpMessageConverter: {}" , converter.getClass().getName());
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter jacksonConverter =
                        (MappingJackson2HttpMessageConverter) converter;
                log.info("MappingJackson2HttpMessageConverter: {}" ,jacksonConverter);
                log.info("ObjectMapper: {}" ,jacksonConverter.getObjectMapper());
            }
        });
    }

    @Bean
    public CommonsRequestLoggingFilter logFilter() {
        // logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG
        CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
        loggingFilter.setIncludeClientInfo(true);
        loggingFilter.setIncludeQueryString(true);
        loggingFilter.setIncludeHeaders(true);
        loggingFilter.setIncludePayload(true);
        loggingFilter.setMaxPayloadLength(20480);
        loggingFilter.setBeforeMessagePrefix("RequestLogging START: ");
        loggingFilter.setAfterMessagePrefix("RequestLogging END: ");
        return loggingFilter;
    }

    /**
     * 自定义 LOG Filter, 需添加 SSE & WebSocket 支持
     * @return
     */
    // @Bean
    // public FilterRegistrationBean<RequestResponseLoggingFilter> loggingFilter() {
    //     FilterRegistrationBean<RequestResponseLoggingFilter> registration = new FilterRegistrationBean<>();
    //     registration.setFilter(new RequestResponseLoggingFilter());
    //     registration.addUrlPatterns("/*");
    //     registration.setOrder(1); // 设置过滤器顺序
    //     return registration;
    // }
}

